﻿/* SharePoint Console Project
 * Http://SharePointConsole.codeplex.com
 * Copyright Jean-Alain Baeyens (2009)
 * Software under Microsoft Reciprocal License (Ms-RL)
 */

using System;

using Microsoft.SharePoint;
using Microsoft.Office.Server.Search.Administration;
using Sharepoint.Console.Commands;

namespace Sharepoint.Console.Search
{
    class AddCrawledPropertyToIndex : BaseCommande
    {
        public override string ShortHelp
        {
            get
            {
                return "Add a crawled property ton index";
            }
        }

        public override string LongHelp
        {
            get
            {
                return "This property will be crawled next tim crawl run.";
            }
        }

        public override string Syntax
        {
            get
            {
                return "AddCrawledPropertyToIndex 'PropertyName'";
            }
        }

        public override string Execute(string[] args)
        {
            if (args.Length != 2) return string.Format("Invalide syntax. Use {0}", Syntax);

            try
            {
                string propertyName = args[1];

                SharepointHelper helper = new SharepointHelper();
                CrawledProperty cprop = helper.GetCrawledProperty(this.SharepointConnection, propertyName);

                if (cprop == null)
                {
                    return "Property not found";
                }
                else
                {
                    cprop.IsMappedToContents = true;
                    cprop.Update();
                    return "Crawled Property added to index";
                }
            }
            
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
